//创建一个http服务器
// const http = require('http')
// const server = http.createServer((request,response) =>{
//     console.log('there is a request')
//     response.end('a response from server')
// })
// server.listen(3000)


//显示一个首页
const http = require('http')
const fs = require('fs')
const server = http.createServer((request,response) =>{
   const {url,method} = request
   if(url === '/' && method === "GET"){
        fs.readFile('index.html',(err,data)=>{
            if(err){
                response.writeHead(500,{'Content-Type':'text/plain;charset=utf-8'})
                response.end('500,服务器错误');
                return;
            }
            response.statusCode = 200;
            response.setHeader("Content-Type",'text/html');
            
        })
   }else if(url === '/users' && method === "GET"){
        response.writeHead(200,{'Content-Type':'application/json'});
        response.end(JSON.stringify([{name:'Tom',age:20}]))
   }else if(url === '/img' && method === "GET"){
    const rs = fs.createReadStream('./img.png')
    const ws = fs.createWriteStream('./img3.png')
    rs.pipe(ws)
    response.setHeader("Content-Type",'text/plain;charset=utf-8');
    response.end('完成了')
    }else{
       response.statusCode = 404;
       response.setHeader("Content-Type",'text/plain;charset=utf-8');
       response.end('404,页面没有找到')
   }
})
server.listen(3000)